package tgc.edu.demo.jkxt.controller;

import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import tgc.edu.demo.jkxt.custom.AjaxResult;
import tgc.edu.demo.jkxt.custom.BaseController;
import tgc.edu.demo.jkxt.custom.TablePageable;

import tgc.edu.demo.jkxt.entity.TbClass;

import tgc.edu.demo.jkxt.form.TbClassForm;
import tgc.edu.demo.jkxt.service.TbClassService;

@RestController
@RequestMapping(value="/banji")
@CrossOrigin
public class TbClassController extends BaseController<TbClass, Integer, TbClassForm>{
	
    
	@SuppressWarnings("serial")
	public Specification<TbClass> buildSpec(TbClassForm form) {
		Specification<TbClass> spec=new Specification<TbClass>() {

			@Override
			public Predicate toPredicate(Root<TbClass> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				String search = form.getSearch();
				if(StringUtils.hasText(search)) {					
					  HashSet<Predicate> rules2=new HashSet<>();
					  rules2.add(cb.like(root.get("manager"),"%"+search+"%"));
					  rules2.add(cb.like(root.get("name"),"%"+search+"%"));
					  
					  rules.add(cb.or(rules2.toArray( new Predicate[rules2.size()])));
					  
				}
					return cb.and(rules.toArray(new Predicate[rules.size()]));
				}			
		};
		return spec;
	}
}